﻿#region Using
// System
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

// Csla
using Csla;
#endregion // Using

namespace GACAssemblyExporter.Library
{
    /// <summary>
    /// List of version info objects
    /// </summary>
    [Serializable()]
    public class VersionInfoList: ReadOnlyListBase<VersionInfoList, VersionInfo>
    {
        #region Constructors
        /**********************************************************************
         * Constructors
         *********************************************************************/
        private VersionInfoList() { }
        #endregion // Constructors

        #region Data Portal Methods
        /**********************************************************************
         * Data Portal Methods
         *********************************************************************/
        /// <summary>
        /// Data portal fetch method
        /// </summary>
        /// <param name="criteria"></param>
        private void DataPortal_Fetch(RootDirectoryCriteria criteria)
        {
            if (Directory.Exists(criteria.RootDirectory))
            {
                this.IsReadOnly = false;
                foreach(string versionDir in Directory.GetDirectories(criteria.RootDirectory, string.Concat("*", VersionInfo.SEPARATOR, "*")))
                {
                    this.AddVersionInfo(VersionInfo.Get(versionDir));
                }// for each version directory
                this.IsReadOnly = false;
            }// if the directory exists
        }// of DataPortal_Fetch(RootDirectoryCriteria)
        #endregion // Data Portal Methods

        #region Modifier Methods
        /**********************************************************************
         * Modifier Methods
         *********************************************************************/
        /// <summary>
        /// Add a version info
        /// </summary>
        /// <param name="versionInfo"></param>
        /// <returns></returns>
        internal bool AddVersionInfo(VersionInfo versionInfo)
        {
            bool toReturn = false;

            IEnumerable<VersionInfo> existing = from vi in this
                                                where string.Compare(vi.Name, versionInfo.Name, true) == 0
                                                        && string.Compare(vi.AssemblyId, versionInfo.AssemblyId, true) == 0
                                                select vi;

            if (existing.Count<VersionInfo>() > 0)
            {
                VersionInfo viExisting = existing.FirstOrDefault<VersionInfo>();
                if (viExisting != null)
                {
                    foreach (string path in versionInfo.AssemblyPathList)
                    {
                        if (!viExisting.AssemblyPathList.Contains(path)) { viExisting.AssemblyPathList.Add(path); }
                    }// for each path
                }// if the value is set
                else
                {
                    this.Add(versionInfo);
                    toReturn = true;
                }// if the value isn't set
            }// if there are existing
            else
            {
                this.Add(versionInfo);
                toReturn = true;
            }// if there aren't existing

            return toReturn;
        }// of bool AddVersionInfo(VersionInfo)
        #endregion // Modifier Methods

        #region Factory Methods
        /**********************************************************************
         * Factory Methods
         *********************************************************************/
        /// <summary>
        /// Gets the version info list for a directory
        /// </summary>
        /// <param name="assemblyRootDirectory"></param>
        /// <returns></returns>
        internal static VersionInfoList Get(string assemblyRootDirectory)
        {
            return DataPortal.Fetch<VersionInfoList>(new RootDirectoryCriteria() { RootDirectory = assemblyRootDirectory });
        }// of VersionInfoList Get()
        #endregion // Factory Methods
    }// of class VersionInfoList
}// of namespace GACAssemblyExporter.Library